﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Control
{
    public partial class AddressControl : System.Web.UI.UserControl
    {
        public AddressControl()
        {
            ShowDetailAddress = true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ProvinceCascadeCombox.Group = this.ClientID;
            CityCascadeCombox.Group = this.ClientID;
            AreaCascadeCombox.Group = this.ClientID;
            if (!IsPostBack)
            {
                if (Value > 0 && !String.IsNullOrEmpty(MemberAccount))
                {
                    using(ISession session = SessionFactoryHelper.CreateSession())
                    {
                        MemberAddress addr = session.Get<MemberAddress>(Value);
                        if (addr != null)
                        {
                            ProvinceCascadeCombox.SelectedValue = addr.Province;
                            CityCascadeCombox.SelectedValue = addr.City;
                            AreaCascadeCombox.SelectedValue = addr.Area;
                            address.Text = addr.Address;
                        }
                    }
                }
            }
            else
            {
                

            }
        }

        public string MemberAccount { get; set; }

        public bool ShowDetailAddress { get; set; }

        
        public int Value
        {
            get
            {
                int result = 0;
                Int32.TryParse(AddressId.Value, out result);
                if (IsPostBack && !String.IsNullOrEmpty(MemberAccount))
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        MemberAddress addr = session.Get<MemberAddress>(result);
                        if (addr == null)
                        {
                            addr = new MemberAddress();
                            addr.MemberAccount = MemberAccount;
                        }
                        if (addr != null)
                        {
                            addr.Province = ProvinceCascadeCombox.SelectedValue;
                            addr.City = CityCascadeCombox.SelectedValue;
                            addr.Area = AreaCascadeCombox.SelectedValue;
                            addr.Address = address.Text;
                        }
                        session.SaveOrUpdate(addr);
                        session.Flush();

                        result = addr.Id;
                    }
                }
                return result;
            }
            set
            {
                if (!IsPostBack)
                {
                    if (value > 0 && !String.IsNullOrEmpty(MemberAccount))
                    {
                        using (ISession session = SessionFactoryHelper.CreateSession())
                        {
                            MemberAddress addr = session.Get<MemberAddress>(value);
                            if (addr != null)
                            {
                                ProvinceCascadeCombox.SelectedValue = addr.Province;
                                CityCascadeCombox.SelectedValue = addr.City;
                                AreaCascadeCombox.SelectedValue = addr.Area;
                                address.Text = addr.Address;
                            }
                        }
                    }
                }
                AddressId.Value = value.ToString();
            }
        }
    }
}